package android.pot.com.smartcar.ui.login;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.fragment.CallDialogView;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.TipToast;
import android.pot.com.smartcar.util.RegexUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zhaoshilu on 2017/11/29.
 */
public class FindLoginsPsdActivity extends SwipeBackActivity {

    private TextView tv_getcode;

    private EditText et_phone;

    private EditText et_name;

    private EditText et_code;

    private EditText et_input;

    private EditText et_input_sure;

    private TimerTask timerTask;

    private int second = 60;

    private Timer timer;

    private String verifyCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findlogins_psd);
        initView();
        addListener();
        initData();


    }

    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("忘记密码");
        tv_getcode = (TextView) findViewById(R.id.tv_getcode);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        et_input = (EditText) findViewById(R.id.et_input);
        et_input_sure = (EditText) findViewById(R.id.et_input_sure);
    }

    protected void addListener() {

        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });

        findViewById(R.id.bt_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextStep();
            }
        });
        tv_getcode.setOnClickListener(new ObtainCodeListener());
    }

    protected void initData() {


    }


    private void reSend() {
        second = 60;
        cleanSendTimerTask();
        tv_getcode.setBackgroundResource(R.drawable.selector_black_btn);
        tv_getcode.setEnabled(true);
        et_phone.setEnabled(true);
        tv_getcode.setText("重新发送");
    }

    private void cleanSendTimerTask() {
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cleanSendTimerTask();
    }

    Handler timehandler = new Handler() {
        public void handleMessage(Message msg) {
            if (second != 0) {
                tv_getcode.setEnabled(false);
                et_phone.setEnabled(false);
                tv_getcode.setBackgroundResource(R.drawable.corner_gray_bg);
                second--;
                tv_getcode.setText(second + "");
            } else {
                reSend();
            }
        }

        ;
    };


    private void sendCode(){
        if (TextUtils.isEmpty(et_phone.getText().toString())) {
            TipToast.show( "请输入手机号");
            return;
        } else {
            if (!RegexUtils.checkMobile(et_phone.getText().toString())) {
                TipToast.show("请输入正确手机号");
                return;
            }
        }


        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", et_phone.getText().toString());
        Action.get().sendCode(this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {

                timerTask = new TimerTask() {

                    @Override
                    public void run() {
                        Message msg = new Message();
                        msg.what = 0;
                        timehandler.sendMessage(msg);
                    }
                };

                timer = new Timer();
                tv_getcode.setTextSize(14);
                tv_getcode.setText(second + "");
                timer.schedule(timerTask, 0, 1000);
            }
        });
    }


    public class ObtainCodeListener implements View.OnClickListener {

        @Override
        public void onClick(View arg0) {
            sendCode();
        }
    }

    private void nextStep(){

//        if (TextUtils.isEmpty(et_name.getText().toString())) {
//            TipToast.show("请输入用户名");
//            return;
//        }

        if (TextUtils.isEmpty(et_phone.getText().toString())) {
            TipToast.show( "请输入手机号");
            return;
        } else {
            if (!RegexUtils.checkMobile(et_phone.getText().toString())) {
                TipToast.show( "请输入正确手机号");
                return;
            }
        }

        if (TextUtils.isEmpty(et_code.getText().toString())) {
            TipToast.show("请输入验证码");
            return;
        }

        if (TextUtils.isEmpty(et_input.getText().toString())){
            TipToast.show( "请输入密码");
            return;
        }else {
            if (et_input.getText().toString().length()<6 || et_input.getText().toString().length()>12) {
                TipToast.show("请输入6-12位密码");
                return;
            }
        }

        if (TextUtils.isEmpty(et_input_sure.getText().toString())){
            TipToast.show( "请输入确认密码");
            return;
        }else {
            if (!et_input_sure.getText().toString().equals(et_input.getText().toString())) {
                TipToast.show("两次密码不一致");
                return;
            }
        }


        Map<String,String> map = new HashMap<>();
        map.put("password",et_input.getText().toString());
        map.put("password_confirm",et_input_sure.getText().toString());
        map.put("auth_code",et_code.getText().toString());
        map.put("phone",et_phone.getText().toString());
//        map.put("name", et_name.getText().toString());
        LoadingTool.StartLoading(FindLoginsPsdActivity.this);
        Action.get().forget_password(FindLoginsPsdActivity.this, map, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                LoadingTool.EndLoading(FindLoginsPsdActivity.this);
                CallDialogView dialogView = new CallDialogView(FindLoginsPsdActivity.this,"密码修改成功");
                dialogView.show(getSupportFragmentManager(),"dialogView");
                dialogView.setMethodClickListener(new CallDialogView.OnMethodClickListener() {
                    @Override
                    public void methodClick() {
                        startAct(FindLoginsPsdActivity.this,LoginsActivity.class);
                    }
                });

            }
        });



//        Map<String, String> params = new HashMap<String, String>();
//        params.put("name", et_name.getText().toString());
//        params.put("mobile", et_phone.getText().toString());
//
//        LoadingTool.StartLoading(this);
//        Action.get().findLoginPwd_1(this, params, new MyResponseHandler() {
//            @Override
//            public void onSuccess(String message,JSONObject response) {
//                TipToast.show("密码设置成功");
//                LoadingTool.EndLoading(FindLoginPsdActivity.this);
//                FindLoginPwdModel model = GsonUtil.getObject(response.toString(), FindLoginPwdModel.class);
//                model.setCode(et_code.getText().toString());
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("Model",model);
//                startAct(FindLoginPsdActivity.this,SetLoginPsdActivity.class,bundle);
//            }
//        });


    }


}